package com.wzd.imservice.controller;

import com.wzd.commonutils.JwtUtils;
import com.wzd.commonutils.R;
import com.wzd.imservice.entity.Mine;
import com.wzd.imservice.service.MineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName MineController
 * @Author lofxve
 * @Date 2021/4/16 11:27
 * @Version 1.0
 */
@RestController
@Api(tags = "IM用户服务")
@RequestMapping("/eduim/mine")
public class MineController {
    @Autowired
    private MineService mineService;

    @ApiOperation("增加用户")
    @PostMapping("saveMine")
    public R saveMine(@RequestBody Mine mine) {
        R save = mineService.save(mine);
        return save;
    }

    @ApiOperation("修改用户")
    @PostMapping("updateMine")
    public R updateMine(@RequestBody Mine mine) {
        if (mine.getId() == null) {
            return R.error();
        }
        R save = mineService.updateMine(mine);
        return save;
    }

    @ApiOperation("删除用户")
    @PostMapping("deleteMineById")
    public R deleteMineById(@RequestBody String id) {
        R save = mineService.deleteById(id);
        return save;
    }

    @ApiOperation("根据id获取用户")
    @GetMapping("getMineById")
    public R getMineById(HttpServletRequest request) {
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        if (StringUtils.isEmpty(memberIdByJwtToken)) {
            return R.error().code(28004).message("请登录");
        }
        Mine mine = mineService.getMineById(memberIdByJwtToken);
        if (mine != null) {
            return R.ok().data("mine", mine);
        } else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("获取所有用户")
    @GetMapping("getAllMine")
    public R getAllMine() {
        List<Mine> mine = mineService.getAllMine();
        if (mine != null) {
            return R.ok().data("mine", mine);
        } else {
            return R.error().message("数据不存在");
        }
    }
}
